package Dao;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;


import Bean.BeanSc;
import Bean.BeanStudent;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.ResultSetExtractor;

import Bean.BeanOrder;
import Bean.BeanUser;

public class ScDao {
    private JdbcTemplate jdbcTemplate;

    public JdbcTemplate getJdbcTemplate() {
        return jdbcTemplate;
    }

    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }
    public void setSc(BeanSc beanSc){
        String sql="update student_class set state=? where student_id=? and class_id=? and day=?";
        this.jdbcTemplate.update(sql,new Object[] {beanSc.getState(),beanSc.getStudent_id(),beanSc.getClass_id(),beanSc.getDay()});
    }
    public void insertSc(int class_id,String day){
        String sql="select student_id from order_details where class_id=?";
        List<Integer> student_ids=new ArrayList<>();
        this.jdbcTemplate.query(sql, new ResultSetExtractor<BeanStudent>() {
            @Override
            public BeanStudent extractData(ResultSet rs) throws SQLException, DataAccessException {
                // TODO Auto-generated method stub
                while(rs.next()) {
                    student_ids.add(rs.getInt(1));
                }
                return null;
            }},class_id);
        sql="insert into student_class(student_id,class_id,day) values(?,?,?)";
        for(Integer student_id:student_ids){
            this.jdbcTemplate.update(sql,new Object[] {student_id,class_id,day});
        }
    }
    public ArrayList<BeanSc> getSc(BeanSc beanSc){
        String sql="select sc_id,student_id,class_id,state,day from student_class where class_id=? and day=?";
        ArrayList<BeanSc> beanScs =new ArrayList<BeanSc>();
        this.jdbcTemplate.query(sql, new ResultSetExtractor<BeanSc>() {
            @Override
            public BeanSc extractData(ResultSet rs) throws SQLException, DataAccessException {
                // TODO Auto-generated method stub
                while(rs.next()) {
                    BeanSc sc=new BeanSc();
                    sc.setSc_id(rs.getInt(1));
                    sc.setStudent_id(rs.getInt(2));
                    sc.setClass_id(rs.getInt(3));
                    sc.setState(rs.getInt(4));
                    sc.setDay(rs.getString(5));
                    beanScs.add(sc);
                }
                return null;
            }},beanSc.getClass_id(),beanSc.getDay());
        for(BeanSc sc:beanScs){
            sql="select student_id,student_name,student_age from student where student_id=?";
            BeanStudent student=this.jdbcTemplate.query(sql, new ResultSetExtractor<BeanStudent>() {
                @Override
                public BeanStudent extractData(ResultSet rs) throws SQLException, DataAccessException {
                    // TODO Auto-generated method stub
                    if(rs.next()) {
                        BeanStudent student1=new BeanStudent();
                        student1.setStudent_id(rs.getInt(1));
                        student1.setStudent_name(rs.getString(2));
                        student1.setStudent_age(rs.getInt(3));
                        return student1;
                    }
                    return null;
                }},sc.getStudent_id());
            sc.setStudent(student);
        }
        return beanScs;
    }
}

